package com.exchange.employee.controller;

import com.exchange.employee.dto.AddArticleDto;
import com.exchange.employee.dto.EsQueryDto;
import com.exchange.employee.service.ArticleService;
import com.exchange.group.AddGroup;
import com.exchange.group.SelectGroup;
import com.exchange.utils.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RefreshScope
@RequestMapping("/system/article")
@Api(value = "文章管理", tags = "文章管理接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/getArticleList")
    @ApiOperation("查询文章信息")
    public CommonResult getArticleList(@RequestBody @Validated(value = SelectGroup.class) EsQueryDto queryDto){
        try {
            return articleService.getArticleList(queryDto);
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.error("服务异常，请联系管理员");
        }
    }

    @PostMapping("/addArticle")
    @ApiOperation("新增文章信息")
    public CommonResult addArticle(@RequestBody @Validated(value = AddGroup.class) AddArticleDto articleDto){
        return articleService.addArticle(articleDto);
    }
}
